package com.molyfun.parents.activity.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.StringResponse;
import com.molyfun.parents.rest.bean.UploadUrlRequest;
import com.molyfun.parents.utils.UrlUtils;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImageViewerActivity extends BaseActivity {

    @InjectView(R.id.iv_photo)
    PhotoView mPhotoView;

    public static final String FLAG_IMAGE_VIEW_URL_TRANSFER = "image_url";
    private String mUrl;

    public static void startActivity(Context context, String imageUrl){
        Intent intent = new Intent();
        intent.setClass(context, ImageViewerActivity.class);
        intent.putExtra(FLAG_IMAGE_VIEW_URL_TRANSFER, imageUrl);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_viewer);
        ButterKnife.inject(this);

        mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                ImageViewerActivity.this.finish();
            }
        });

        Intent intent = getIntent();
        if(intent != null){
            mUrl = intent.getStringExtra(FLAG_IMAGE_VIEW_URL_TRANSFER);
            if(!TextUtils.isEmpty(mUrl)){
                if(UrlUtils.isExpired(mUrl)){
                    showProgressDialog();
                    UploadUrlRequest uploadUrlRequest = new UploadUrlRequest();
                    uploadUrlRequest.setUrl(UrlUtils.getNewUrl(mUrl));
                    Call<StringResponse> call = MFPApplication.getInstance().getRestClient().updateMediaUrl(uploadUrlRequest);
                    call.enqueue(new Callback<StringResponse>() {
                        @Override
                        public void onResponse(Call<StringResponse> call, Response<StringResponse> response) {
                            dismissProgressDialog();
                            mUrl = response.body().getData();
                            Picasso.with(ImageViewerActivity.this)
                                    .load(mUrl)
                                    .into(mPhotoView);
                        }

                        @Override
                        public void onFailure(Call<StringResponse> call, Throwable t) {
                            dismissProgressDialog();
                            toast("获取图片失败");
                        }
                    });
                }else{
                    Picasso.with(this)
                            .load(mUrl)
                            .into(mPhotoView);
                }
            }else{
                toast("图片不存在");
                finish();
            }
        }

    }

}